import re
from datetime import datetime

from common import bin_storage, file_mgmt, bootstrap


# First, read the older file to get {"code": {"latest":"A", "special":"B","f1_time":C, "shareholder":"D", "f4_time":E}}
# Then, read the newer file by row, find the value set by its key, update the interesting 3 items,
# At last, output the sets with a subset with length >1 which indicates a change
def get_update(a, b):
    return b if a != b else False


# for each value in one set, if its {valid chars} is different from its counterpart,
# wrap them in a list or append to the list if exists
def combine(so, sn):
    for i in {'special', 'shareholder', 'gong', 'wei'}:
        to = re.sub('@[^@]*$', '', so[i][-1])
        to = re.sub('\s', '', to)
        tn = re.sub('@[^@]*$', '', sn[i][-1])
        tn = re.sub('\s', '', tn)
        if tn != to:
            so[i].append(sn[i][-1])
            offset = len(so[i]) - 3  # keep the latest 3 elements of "latest":[a,b,c]
            if offset > 0:
                del so[i][:offset]
    return so


def run(latest_bin, latest_ana, save_bin_name=None, g_path=''):
    # read the old and new files, ready to iterate rows
    bin = bin_storage.Bin_storage()
    # bin_bare
    do = bin.read(latest_bin)
    dn = bootstrap.run(latest_ana)[1]
    #    if not save_bin_name:
    save_bin_bare_name = save_bin_name + 'bin_bare/' + datetime.today().strftime('%Y-%m-%d')
    save_bin_name = save_bin_name + 'bin/' + datetime.today().strftime('%Y-%m-%d')

    # put DictReader object into dict with a bit reformat

    # {code:{'special':'','f1_time':'','shareholder':'','f4_time':'','gong':'','wei':''},}

    # identify emerged codes and the conjunction
    for k in dn:
        # take pairs from the two dicts given the keys chosen and 'combine' them
        if k in do:
            do[k] = combine(do[k], dn[k])
        else:
            do[k] = dn[k]
    # the order of calling 'save' & 'save_bare' is vital because 'do' changes in 'save'
    bin.save_bare(save_bin_bare_name, do)
    file_mgmt.push(dir=g_path + 'bin_bare', ext="bin")
    bin.save(save_bin_name, do)
    file_mgmt.push(dir=g_path + 'bin', ext="bin")



# {code:{'gong':[],'wei':[],'special':[],'shareholder':[]},}

if __name__ == '__main__':
    # run('ana/ana_2019-01-17.csv','ana/ana_2019-01-18.csv','2019-01-18')
    bin = bin_storage.Bin_storage()
    # print(bin.read('bin/2019-05-06.bin'))
    latest_bin = file_mgmt.push(dir="bin", ext='bin')[1]
    print(latest_bin)
    run(latest_bin, 'ana/ana_2019-06-05.csv', '2019-06-05')
    d = bin.read('bin/2019-06-05.bin')
    for i in d:
        a = d[i]
        for j in a:
            if len(a[j]) > 1:
                print(a)
                break
